import dd from 'gdt-jsapi'; // 按需引入方法
import dd2 from 'dingtalk-jsapi'; // 按需引入方法
import { scanCode } from '@tarojs/taro';
import { ACCESS } from '/@/api/modules/qrcode/open';
import { navigateTo } from '@tarojs/taro';

/**
 * 根据是否是钉钉环境，调用不同api进行扫一扫
 * @returns 扫一扫结果
 */
function scan(): Promise<string> {
  if (window.navigator.userAgent.includes('DingTalk')) {
    return new Promise((resolve) => {
      dd2.scan({
        type: 'qr',
        success: (res) => {
          resolve(res.text);
        },
        fail: () => {},
        complete: () => {},
      });
    });
  } else if (navigator.userAgent.includes('TaurusApp')) {
    return new Promise((resolve) => {
      /**
       * 浙政钉钉扫描
       */
      dd.scan({
        type: 'qrCode',
      }).then((res) => {
        resolve(res.text);
      });
    });
  } else {
    return new Promise((resolve) => {
      /**
       * 其他平台扫描
       */
      scanCode({
        onlyFromCamera: true,
        success: (res) => {
          resolve(res.result);
        },
      });
    });
  }
}

/**
 * 首页和全部应用
 * 二维码扫描
 * 钉钉
 */
function scanMenu() {
  if (window.navigator.userAgent.includes('DingTalk')) {
    dd2.scan({
      type: 'qr',
      success: (res) => {
        scanResult(res.text);
      },
      fail: () => {},
      complete: () => {},
    });
  } else if (navigator.userAgent.includes('TaurusApp')) {
    dd.scan({
      type: 'qrCode',
    }).then((res) => {
      scanResult(res.text);
    });
  } else {
    scanCode({
      onlyFromCamera: true,
      success: (res) => {
        scanResult(res.result);
      },
    });
  }
}

/**
 * 扫描结果处理
 */
function scanResult(result: string) {
  console.log('scanMenu ===> scanResult ===>' + result);
  const url = new URL(result);
  const t = url.searchParams.get('t');
  if (!t) {
    //result 是一个http地址
    if (result.startsWith('http' || 'https')) {
      //自己h5的页面
      if (result.includes('hgy-taro-app')) {
        const appUrl = url.hash.split('#')[1];
        console.log('appUrl', appUrl);
        navigateTo({
          url: appUrl,
        });
      } else {
        //第三方链接
        window.location.href = result;
      }
    } else {
      navigateTo({
        url: '/pages/scan/scan-fail/index?error=unknown',
      });
    }
    return;
  }

  handleByT(t);
}

/**
 * 根据参数t, 进行不同
 * @param t 参数t
 */
function handleByT(t: string) {
  ACCESS({ t: t }).then((res) => {
    if (!res.comResp) {
      navigateTo({
        url: '/pages/scan/scan-fail/index?error=network',
      });
      return;
    }
    if (res.comResp.code === 101) {
      navigateTo({
        url: '/pages/scan/scan-fail/index?error=unknown',
      });
      return;
    }

    if (!res.content) {
      navigateTo({
        url: '/pages/scan/scan-fail/index?error=network',
      });
      return;
    }
    /**
     * 获取参数信息
     */
    const params = res.content.params as {
      id: string;
    };
    switch (res.content?.businessTag) {
      case 'ASSET_DETAIL':
        navigateTo({
          url: `/pages/scan/scan-success/index?assetId=${params.id}`,
        });
        break;
      case 'APP_SCHEDULE_DETAIL':
        navigateTo({
          url: `/pages/swap/my/index?id=${params.id}`,
        });
        break;
      case 'AREA_DETAIL':
        navigateTo({
          url: `/pages/scan/workspace/index?id=${params.id}`,
        });
        break;
    }
  });
}

export { scan, scanMenu };
